package com.dupf.product.web.controller.product;

import com.dupf.common.base.BaseController;
import com.dupf.product.common.constant.ProductResult;
import com.dupf.product.common.constant.ProductResultConstant;
import com.dupf.product.dao.model.ProductCollectRecord;
import com.dupf.product.dao.model.ProductCollectRecordExample;
import com.dupf.product.rpc.api.ProductCollectRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品controller
 * Created by dupf on 2018/3/20.
 */
@Controller
@Api(value = "产品管理", description = "产品管理")
@RequestMapping("/product/collect")
public class ProductCollectRecordController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductCollectRecordController.class);

    @Autowired
    private ProductCollectRecordService productCollectRecordService;

    @ApiOperation(value = "产品采集列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("currentUserId");
        ProductCollectRecordExample productCollectRecordExample = new ProductCollectRecordExample();
        productCollectRecordExample.createCriteria().andUserIdEqualTo(userId);
        productCollectRecordExample.setOrderByClause("id DESC");
        List<ProductCollectRecord> rows = productCollectRecordService.selectByExampleForOffsetPage(productCollectRecordExample, offset, limit);
        long total = productCollectRecordService.countByExample(productCollectRecordExample);
        Map<String, Object> result = new HashMap<>();
        result.put("list", rows);
        result.put("total", total);
        return new ProductResult(ProductResultConstant.SUCCESS, result);
    }
}